package com.mdd.admin.service.image.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import com.mdd.admin.service.image.IGoodsImageService;
import com.mdd.admin.validate.image.GoodsImageParam;
import com.mdd.admin.vo.image.GoodsImageDetailVo;
import com.mdd.common.entity.image.GoodsImage;
import com.mdd.common.mapper.image.GoodsImageMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * 服务轮播图实现类
 */
@Slf4j
@Service
public class GoodsImageServiceImpl extends ServiceImpl<GoodsImageMapper, GoodsImage> implements IGoodsImageService {

    @Resource
    GoodsImageMapper goodsImageMapper;

    /**
     * 服务轮播图详情
     *
     * @param id 主键参数
     * @return GoodsImage
     */
    @Override
    public GoodsImageDetailVo detail(Integer id) {
        GoodsImage model = goodsImageMapper.selectOne(
                new QueryWrapper<GoodsImage>()
                        .eq("id", id)
                        .last("limit 1"));

        Assert.notNull(model, "数据不存在");

        GoodsImageDetailVo vo = new GoodsImageDetailVo();
        BeanUtils.copyProperties(model, vo);
        return vo;
    }

    /**
     * 服务轮播图新增
     *
     * @param goodsImageParam 参数
     */
    @Override
    public void add(GoodsImageParam goodsImageParam) {
        GoodsImage model = new GoodsImage();
        model.setGoodsId(goodsImageParam.getGoodsId());
        model.setUri(goodsImageParam.getUri());
        goodsImageMapper.insert(model);
    }

    /**
     * 服务轮播图编辑
     *
     * @param goodsImageParam 参数
     */
    @Override
    public void edit(GoodsImageParam goodsImageParam) {
        GoodsImage model = goodsImageMapper.selectOne(
                new QueryWrapper<GoodsImage>()
                        .eq("id", goodsImageParam.getId())
                        .last("limit 1"));

        Assert.notNull(model, "数据不存在!");

        model.setId(goodsImageParam.getId());
        model.setGoodsId(goodsImageParam.getGoodsId());
        model.setUri(goodsImageParam.getUri());
        goodsImageMapper.updateById(model);
    }

    /**
     * 服务轮播图删除
     *
     * @param id 主键ID
     */
    @Override
    public void del(Long id) {
        GoodsImage model = goodsImageMapper.selectOne(
                new QueryWrapper<GoodsImage>()
                        .eq("id", id)
                        .last("limit 1"));

        Assert.notNull(model, "数据不存在!");

        goodsImageMapper.delete(new QueryWrapper<GoodsImage>().eq("id", id));
    }

    @Override
    public void batchAdd(Long goodsId, List<String> urlList) {
        if (CollectionUtils.isEmpty(urlList) || goodsId == null) {
            return;
        }
        List<GoodsImage> list = Lists.newArrayList();
        for (String url : urlList) {
            GoodsImage image = new GoodsImage();
            image.setGoodsId(goodsId);
            image.setUri(url);
            list.add(image);
        }
        super.saveBatch(list);
    }

    @Override
    public List<GoodsImage> listByGoodsId(Long goodsId) {
        if (null == goodsId) {
            return Lists.newArrayList();
        }
        LambdaQueryWrapper<GoodsImage> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(GoodsImage::getGoodsId, goodsId);
        return super.list(lambdaQueryWrapper);
    }

    @Override
    public void delByGoodIds(Long goodsId) {
        LambdaQueryWrapper<GoodsImage> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(GoodsImage::getGoodsId, goodsId);
        super.remove(lambdaQueryWrapper);
    }

}
